<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="login-title">欢迎登录</h1>
        <p class="login-subtitle">AI非代码应用生成平台</p>
      </div>

      <a-form :model="formState" name="login-form" class="login-form" @finish="handleSubmit">

        <a-form-item name="account" :rules="[{ required: true, message: '请输入用户名!' }]">
          <a-input v-model:value="formState.account" size="large" placeholder="用户名" />
        </a-form-item>


        <a-form-item name="password" :rules="[{ required: true, message: '请输入密码!' }]">
          <a-input-password v-model:value="formState.password" size="large" placeholder="密码" />
        </a-form-item>


        <a-form-item>
          <div class="form-options">
            <a-checkbox v-model:checked="formState.remember">记住我</a-checkbox>
            <a class="forgot-link">忘记密码？</a>
          </div>
        </a-form-item>

        <a-form-item>
          <a-button type="primary" html-type="submit" size="large" class="login-button" :loading="loading" block>
            登录
          </a-button>
        </a-form-item>

        <div class="register-link">
          还没有账号？
          <router-link to="/user/register" class="link">立即注册</router-link>
        </div>
      </a-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { userLogin } from '@/api/userController'
import { useLoginUserStore } from '@/stores/loginUser'

const { fetchLoginUser } = useLoginUserStore()
// 定义组件名称
defineOptions({
  name: 'UserLogin',
})

const router = useRouter()
const loading = ref(false)

const formState = reactive<API.UserLoginRequest>({
  account: '',
  password: '',
  remember: false,
})

const handleSubmit = async (values: FormState) => {
  loading.value = true
  try {
    console.log('登录表单数据:', values)
    // 调用登录 API
    const res = await userLogin(formState)
    if (res.code === 0) {
      message.success('登录成功！')

      // 全局状态管理
      await fetchLoginUser()

      // 登录成功后跳转到首页
      router.push('/')
    }
  } catch (error) {
    message.error('登录失败，' + error.message)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 8px 0;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.login-form {
  width: 100%;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.forgot-link {
  color: #1890ff;
  text-decoration: none;
  font-size: 14px;
}

.forgot-link:hover {
  color: #40a9ff;
}

.login-button {
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
}

.register-link {
  text-align: center;
  margin-top: 24px;
  font-size: 14px;
  color: #666;
}

.link {
  color: #1890ff;
  text-decoration: none;
  font-weight: 500;
}

.link:hover {
  color: #40a9ff;
}

:deep(.ant-input-affix-wrapper) {
  border-radius: 8px;
  height: 48px;
}

:deep(.ant-input) {
  font-size: 14px;
}

:deep(.ant-form-item) {
  margin-bottom: 20px;
}
</style>
